﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace benefit.Controllers
{
    public class GroupXingNengController : BenefitHelper.Tools.BaseControl
    {
        //
        // GET: /GroupXingNeng/

        public ActionResult Index(int uid, string pdate)
        {
            Common.Web.Cookie cookie = new Common.Web.Cookie();
            //   cookie.SetCookie("querydate", pdate, 20);
            ViewBag.groupquerydate = cookie.GetCookie("groupquerydate");
            cookie.SetCookie("groupquerydate", cookie.GetCookie("groupquerydate"), 24);
            string s = cookie.GetCookie("querydate");
            ViewBag.datequery = cookie.GetCookie("querydate");
            int operId = Convert.ToInt32(cookie.GetCookie("LoginUserId"));
            BenefitHelper.Sys.Groups group = new BenefitHelper.Sys.Groups().GetGroup(uid, db);
            ViewBag.group = group;
            if (BenefitHelper.Sys.VisitPrower.HasGroupPrower(operId, uid, db))
            {
                if (pdate.Length > 4)//查询月数据
                {
                    DateTime nowmonth = Convert.ToDateTime(Convert.ToInt32(pdate.Substring(0, 4)) + "-" + Convert.ToInt32(pdate.Substring(4, 2)) + "-01");

                    ViewBag.gid = uid;
                    ViewBag.pdate = pdate;
                    ViewBag.querymonth = pdate.Substring(4, 2);
                    ViewBag.queryyear = pdate.Substring(0, 4);
                    string js = BenefitHelper.Tools.ChartAtManager.MonthBenefitString(uid, nowmonth.Month, nowmonth.Year, db);
                    js += BenefitHelper.Tools.ChartAtManager.GroupPie(uid, nowmonth.Month, nowmonth.Year, db);
                    List<BenefitHelper.View.OperatorMonthSum> changes = new BenefitHelper.View.OperatorMonthSum().GetGroupOperatorDayChange(uid, nowmonth.Month, nowmonth.Year, db);

                    BenefitHelper.Data.Month.GroupMonthSum groupmonthsum = new BenefitHelper.Data.Month.GroupMonthSum();
                    groupmonthsum = groupmonthsum.GetGroupMonthSum(uid, nowmonth.Month, nowmonth.Year, db);
                    ViewBag.groupmonthsum = groupmonthsum;//本月总值

                    BenefitHelper.Data.Year.GroupAvgYear groupavgyear = new BenefitHelper.Data.Year.GroupAvgYear();
                    groupavgyear = groupavgyear.GetGroupAvgYear(uid, nowmonth.Year, db);
                    ViewBag.groupavgyear = groupavgyear;//年均值

                    BenefitHelper.Data.Month.GroupAvgMonth groupavgmonth = new BenefitHelper.Data.Month.GroupAvgMonth();
                    groupavgmonth = groupavgmonth.GetGroupAvgMonth(uid, nowmonth.Month, nowmonth.Year, db);
                    ViewBag.groupavgmonth = groupavgmonth;//小组均值

                    BenefitHelper.Data.Month.CompanyAvgMonth companyavgmonth = new BenefitHelper.Data.Month.CompanyAvgMonth();
                    companyavgmonth = companyavgmonth.GetCompanyAvgMonth(nowmonth.Month, nowmonth.Year, db);
                    ViewBag.companyavgmonth = companyavgmonth;//公司均值


                    DateTime yestermonth = nowmonth.AddMonths(-1);

                    BenefitHelper.Data.Month.GroupMonthSum groupyestermonthsum = new BenefitHelper.Data.Month.GroupMonthSum();
                    groupyestermonthsum = groupyestermonthsum.GetGroupMonthSum(uid, yestermonth.Month, yestermonth.Year, db);
                    ViewBag.groupyestermonthsum = groupyestermonthsum;//上月值

                    DateTime yesteryear = nowmonth.AddYears(-1);

                    BenefitHelper.Data.Month.GroupMonthSum yesteryearsum = new BenefitHelper.Data.Month.GroupMonthSum();
                    yesteryearsum = yesteryearsum.GetGroupMonthSum(uid, yesteryear.Month, yesteryear.Year, db);
                    ViewBag.yesteryearsum = yesteryearsum;//去年同期







                    ViewBag.changes = changes;
                    ViewBag.js = js;
                }
                else//年数据
                {
                    //DateTime nowmonth = Convert.ToDateTime(Convert.ToInt32(pdate.Substring(0, 4)) + "-" + Convert.ToInt32(pdate.Substring(4, 2)) + "-01");

                    ViewBag.gid = uid;
                    ViewBag.pdate = pdate;
                    ViewBag.querymonth = "";
                    ViewBag.queryyear = pdate.Substring(0, 4);
                    string js = BenefitHelper.Tools.ChartAtManager.YearBenefitString(uid, Convert.ToInt32(pdate), db);
                    js += BenefitHelper.Tools.ChartAtManager.YearGroupPie(uid, Convert.ToInt32(pdate), db);
                    List<BenefitHelper.View.OperatorYearSum> changes = new BenefitHelper.View.OperatorYearSum().GetGroupOperatorYearChange(uid, Convert.ToInt32(pdate), db);


                    BenefitHelper.Data.Year.GroupYearSum groupmonthsum = new BenefitHelper.Data.Year.GroupYearSum();
                    groupmonthsum = groupmonthsum.GetGroupYearSum(uid, Convert.ToInt32(pdate), db);
                    ViewBag.groupmonthsum = groupmonthsum;//小组年累计

                    BenefitHelper.Data.Year.GroupAvgYear groupavgyear = new BenefitHelper.Data.Year.GroupAvgYear();
                    groupavgyear = groupavgyear.GetGroupAvgYear(uid, Convert.ToInt32(pdate), db);
                    ViewBag.groupavgyear = groupavgyear;//年均值

                    BenefitHelper.Data.Year.GroupAvgYear groupavgmonth = new BenefitHelper.Data.Year.GroupAvgYear();
                    groupavgmonth = groupavgmonth.GetGroupAvgYear(uid, Convert.ToInt32(pdate), db);
                    ViewBag.groupavgmonth = groupavgmonth;//小组年均值

                    BenefitHelper.Data.Year.CompanyYearAvg companyavgmonth = new BenefitHelper.Data.Year.CompanyYearAvg();
                    companyavgmonth = companyavgmonth.GetCompanyAvgYear(Convert.ToInt32(pdate), db);
                    ViewBag.companyavgmonth = companyavgmonth;//公司均值



                    ////////////////////////////////////////////
                    //DateTime yestermonth = nowmonth.AddMonths(-1);

                    BenefitHelper.Data.Year.GroupYearSum groupyestermonthsum = new BenefitHelper.Data.Year.GroupYearSum();
                    groupyestermonthsum = groupyestermonthsum.GetGroupYearSum(uid, Convert.ToInt32(pdate), db);
                    ViewBag.groupyestermonthsum = groupyestermonthsum;//上月值

                    //    DateTime yesteryear = nowmonth.AddYears(-1);

                    //BenefitHelper.Data.Year.GroupYearSum yesteryearsum = new BenefitHelper.Data.Year.GroupYearSum();
                    //yesteryearsum = yesteryearsum.GetGroupYearSum(uid, Convert.ToInt32(pdate) - 1, db);
                    ViewBag.yesteryearsum = null;//去年







                    ViewBag.changes = changes;
                    ViewBag.js = js;
                }
            }
            else
            {
                db.Dispose();
                return Redirect("/Error/HasNoPrower/" + uid + "/" + pdate);
            }
            return View();
        }

    }
}
